package com.scommunity.admin.controller;

import com.scommunity.common.ResponseResult;
import com.scommunity.domain.Help;
import com.scommunity.domain.HelpItem;
import com.scommunity.service.IHelpItemService;
import com.scommunity.service.IHelpService;
import com.scommunity.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/admin/help")
public class HelpController{

    @Autowired
    private IHelpService helpService;
    @Autowired
    private IHelpItemService helpItemService;

    @Autowired
    private SnowflakeIdWorker snowflakeIdWorker;

    /**
     * 后端根据help类型查询对应项目列表
     * @param helpId
     * @return
     */
    @GetMapping("/listbyhelpid")
    public ResponseResult listByHelpId(Long helpId){
        List<HelpItem> helpItemList = helpItemService.listByHelpId(helpId);
        return ResponseResult.success(helpItemList);
    }


    @GetMapping("/list")
    public ResponseResult list(){
        List<HelpItem> helpItemList = helpService.getList();
        return ResponseResult.success(helpItemList);
    }

    /**
     * 添加时候查询种类
     * @return
     */
    @GetMapping("/helplist")
    public ResponseResult helpList(){
        List<Help> helpList = helpService.list(null);
        return ResponseResult.success(helpList);
    }

    /**
     * 添加便民项
     * @param helpItem
     * @return
     */
    @PostMapping("/additem")
    public ResponseResult addItem(@RequestBody HelpItem helpItem){
        helpItem.setItemId(snowflakeIdWorker.nextId());
        helpItemService.save(helpItem);
        return ResponseResult.success();
    }

    /**
     * 添加便民类型
     * @param help
     * @return
     */
    @PostMapping("/add")
    public ResponseResult add(@RequestBody Help help){
        help.setHelpId(snowflakeIdWorker.nextId());
        helpService.save(help);
        return ResponseResult.success();
    }

    @PostMapping("/update")
    public ResponseResult update(@RequestBody HelpItem helpItem){
        helpItemService.updateById(helpItem);
        return ResponseResult.success();
    }

    @PostMapping("/upload")
    public ResponseResult upload(MultipartFile file){
        String itemPic = helpItemService.upload(file);
        return ResponseResult.success(itemPic);
    }



}
